//
//  HotDetailViewController.m
//  liwushuo
//
//  Created by qf1 on 16/4/15.
//  Copyright (c) 2016年 ZJH. All rights reserved.
//

#import "HotDetailViewController.h"

@interface HotDetailViewController ()<WKNavigationDelegate,WKUIDelegate,UIWebViewDelegate>

@end

@implementation HotDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在加载";
    hud.detailsLabelText = @"请稍后";
    [hud show:YES];
    WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc] init];
    _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 20, theScreenWidth, theScreenHeight - 20) configuration:configuration];
//    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, theScreenWidth, theScreenHeight - 20)];
    _webView.navigationDelegate = self;
    _webView.UIDelegate = self;
//    _webView.delegate = self;
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_webStr]]];
    _webView.alpha = 0;
    [self.view addSubview:_webView];
    [self.view addSubview:({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, theScreenWidth, 20)];
        view.backgroundColor = [UIColor blackColor];
        view;
    })];
    
    UIView * topView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, theScreenWidth, 40)];
    topView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:topView];
    UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [backBtn setImage:[UIImage imageNamed:@"back"]forState:UIControlStateNormal];
    backBtn.frame = CGRectMake(10, 13, 40, 30);
    [backBtn setTintColor:[UIColor whiteColor]];
    [backBtn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:backBtn];
    
}

- (void)btnAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    [webView evaluateJavaScript:@" document.getElementsByClassName('purchase-content')[0].remove()" completionHandler:nil];
    [webView evaluateJavaScript:@" document.getElementsByClassName('download-content download-content-lg')[0].remove()" completionHandler:nil];
//    [webView evaluateJavaScript:@" document.getElementsByIdName('share-tip')[0].remove()" completionHandler:nil];
    for (int i = 0; i < 2; i ++) {
        [webView evaluateJavaScript:@" document.getElementsByClassName('wrapper download-banner')[0].remove()" completionHandler:nil];
    }

    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    sleep(3);
    _webView.alpha = 1;
}

//-(void)webViewDidFinishLoad:(UIWebView *)webView{
//    
//    NSString *JsToGetHTMLSource = @"document.getElementsByTagName('html')[0].innerHTML";
//    NSString *HTMLSource = [webView stringByEvaluatingJavaScriptFromString:JsToGetHTMLSource];
//    NSLog(@"%@",HTMLSource);
//    
//    
//    //    NSString *jsString = @"var testa = document.getElementById('content').children[0];";
//    //
//    //    NSString *jsStringRemove = @"testa.parentNode.removeChild(testa)";
//    
//    //编写webView的JS
//    NSString *jsStringRemove = @"document.getElementByClass('purchase-content').children[0].parentNode.removeChild(document.getElementByClass('purchase-content').children[0])";
//    [webView stringByEvaluatingJavaScriptFromString:
//     jsStringRemove
//     ];
//}

- (void)viewWillAppear:(BOOL)animated{
    [self.navigationController.navigationBar setHidden:YES];
    [self.tabBarController.tabBar setHidden:YES];
    self.tabBarController.tabBar.translucent = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
